<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>配置页面</title>
  <style>
    .config-row {
      margin-bottom: 10px;
      padding: 5px;
      border: 1px solid #ccc;
    }

    #script-output {
      background: #f0f0f0;
      padding: 10px;
      margin-top: 20px;
      white-space: pre-wrap;
      height: 200px;
      overflow-y: auto;
    }
  </style>
</head>
<body>
  <h2>主配置</h2>
  <p>
    <a href="{{ url_for('dingtalk') }}">配置钉钉推送</a> |
    <a href="{{ url_for('layout') }}">配置版型</a>
  </p>

  <form method="post" action="{{ url_for('save') }}">
    <div id="config-container">
      {% for config in saved_config %}
      <div class="config-row">
        串口:
        <select name="port">
          {% for port in serial_ports %}
            <option value="{{ port }}" {% if config.port == port %}selected{% endif %}>{{ port }}</option>
          {% endfor %}
        </select>

        版型:
        <select name="board">
          {% for key in layout_options %}
            <option value="{{ key }}" {% if config.board == key %}selected{% endif %}>{{ key }}</option>
          {% endfor %}
        </select>

        日志路径:
        <input type="text" name="log_file" value="{{ config.log_file }}">

        推送:
        <select name="push">
          {% for group_name in push_options %}
            <option value="{{ group_name }}" {% if config.push == group_name %}selected{% endif %}>{{ group_name }}</option>
          {% endfor %}
        </select>

        开启:
        <input type="checkbox" name="enabled" {% if config.enabled %}checked{% endif %}>
      </div>
      {% endfor %}
    </div>

    <button type="button" onclick="addRow()">新增</button>
    <button type="submit">保存主配置</button>
  </form>

  <!-- 新增：运行配置 -->
  <h2>运行配置</h2>
  <form method="post" action="{{ url_for('save_run_config') }}">
    <div class="config-row">
      周期：
      <input type="number" name="interval" value="{{ run_config.interval }}" min="1" required>
      分钟
    </div>
    <div class="config-row">
      日志上传地址：
      <input type="text" name="upload_url" value="{{ run_config.upload_url }}" style="width: 400px;" required>
    </div>
    <button type="submit">保存运行配置</button>
  </form>

  <!-- 新增：重启检测配置 -->
  <h2>重启检测配置</h2>
  <form method="post" action="{{ url_for('save_reboot_config') }}">
    <div class="config-row">
      电脑名称：
      <input type="text" name="reboot_pc_name" value="{{ reboot_config.pc_name }}" style="width: 200px;">

      推送钉钉群：
      <select name="reboot_push_group">
        {% for group_name in push_options %}
          <option value="{{ group_name }}" {% if reboot_config.push_group == group_name %}selected{% endif %}>{{ group_name }}</option>
        {% endfor %}
      </select>

      启用：
      <input type="checkbox" name="reboot_enabled" {% if reboot_config.enabled %}checked{% endif %}>

      <button type="submit">保存重启配置</button>
    </div>
  </form>

  <!-- 新增：脚本控制按钮 -->
  <br><br>
  <button id="script-toggle" onclick="toggleScript()">启动脚本</button>
  <div id="script-output"></div>

  <script>
    let isRunning = false;

    function toggleScript() {
      const btn = document.getElementById("script-toggle");
      if (!isRunning) {
        fetch("/start-script", { method: "POST" })
          .then(res => res.json())
          .then(data => {
            if (data.status === "started") {
              isRunning = true;
              btn.textContent = "停止脚本";
            } else {
              alert("启动失败: " + data.message);
            }
          });
      } else {
        fetch("/stop-script", { method: "POST" })
          .then(res => res.json())
          .then(data => {
            if (data.status === "stopped") {
              isRunning = false;
              btn.textContent = "启动脚本";
            }
          });
      }
    }

    function pollOutput() {
      fetch("/script-output")
        .then(res => res.json())
        .then(data => {
          const outputDiv = document.getElementById("script-output");
          outputDiv.innerHTML += data.output;
          outputDiv.scrollTop = outputDiv.scrollHeight;
        });
    }

    setInterval(pollOutput, 500); // 每半秒获取一次输出
  </script>

  <script type="text/javascript">
    const layoutOptions = {{ layout_options|tojson }};
    const pushOptions = {{ push_options|tojson }};
    const serialPorts = {{ serial_ports|tojson }};

    function addRow() {
      const container = document.getElementById('config-container');
      const div = document.createElement('div');
      div.className = "config-row";

      let boardOptions = layoutOptions.map(key =>
        `<option value="${key}">${key}</option>`
      ).join('');

      let pushOptionsHtml = pushOptions.map(name =>
        `<option value="${name}">${name}</option>`
      ).join('');

      let portOptions = serialPorts.map(port =>
        `<option value="${port}">${port}</option>`
      ).join('');

      div.innerHTML = `
        串口:
        <select name="port">
          ${portOptions}
        </select>

        版型:
        <select name="board">
          ${boardOptions}
        </select>

        日志路径:
        <input type="text" name="log_file">

        推送:
        <select name="push">
          ${pushOptionsHtml}
        </select>

        开启:
        <input type="checkbox" name="enabled">
      `;
      container.appendChild(div);
    }
  </script>
</body>
</html>